﻿# coding=utf-8
# encoding=utf8
# noinspection PyBroadException
import sys
import datetime
import os
import subprocess
from flask import Flask, render_template, request, redirect, url_for, json, jsonify
from flask_cors import CORS
# from flask_wtf import FlaskForm
# from wtforms import StringField, SubmitField
# from wtforms.validators import DataRequired
# from flask_sqlalchemy import SQLAlchemy
# from repo import GitRepository
from flask_sqlalchemy import SQLAlchemy

from utils import Codezip
# from sqlalchemy import or_, and_
from sshclient import SSHProxy
# from flask_wtf import FlaskForm
from flask_bootstrap import Bootstrap
from Fomclass import addsysopt, addservers, addprojects

app = Flask(__name__)
app.secret_key = os.urandom(24)
CORS(app)
bootstrap = Bootstrap(app)
app.config['SECRET_KEY'] = os.urandom(24)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@127.0.0.1:3306/test1'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
date = datetime.datetime.now().strftime('%Y%m%d%H')
db = SQLAlchemy()
db.init_app(app)

main_t = db.Table('main_t',  # 中间表
                  db.Column('projects_id', db.Integer, db.ForeignKey('projects.id'), primary_key=True),
                  db.Column('host_id', db.Integer, db.ForeignKey('host.id'), primary_key=True),
                  db.Column('type', db.Integer))


class Host(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    hostname = db.Column(db.String(50))
    ipaddr = db.Column(db.String(50))
    Groups = db.Column(db.String(50))
    Desc = db.Column(db.String(50))
    username = db.Column(db.String(50))
    passwd = db.Column(db.String(50))
    port = db.Column(db.String(50))
    check = db.Column(db.String(10), default="off")


class projects(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    project = db.Column(db.String(20))
    repo = db.Column(db.String(100))
    env = db.Column(db.String(100))
    path = db.Column(db.String(200))
    zip = db.Column(db.String(20))
    server = db.relationship('Host', secondary=main_t, backref=db.backref('project'))


@app.route('/')
def hello_world():
    # db.create_all()
    # host1 = Host(hostname='hostname1', ipaddr='127.0.0.1', Groups='group1', Desc='host1', )
    # host2 = Host(hostname='hostname2', ipaddr='127.0.0.2', Groups='group2', Desc='host2', )
    # host3 = Host(hostname='hostname3', ipaddr='127.0.0.3', Groups='group3', Desc='host3', )
    # db.session.add(host1)
    # db.session.add(host2)
    # db.session.add(host3)
    # addproj1 = projects(project="pro1")
    # addproj2 = projects(project="pro2")
    # db.session.add(addproj1)
    # db.session.add(addproj2)
    # # db.session.commit()
    # addproj1.server.append(host2)
    # addproj1.server.append(host1)
    # # addproj2.server.append(host2)
    # # addproj2.server.append(host3)
    # db.session.commit()

    # hostinfo = projects.query.filter(projects.project == 'pro1').first()
    # print(hostinfo)
    # print(type(hostinfo))
    # serverinfo = hostinfo.server
    # for row in serverinfo:
    #     print(row.hostname)
    # project = projects.query.all()
    # print(type(project))
    # data = []
    # for row in project:
    #     for a in row.server:
    #         pro = {row.project: a.hostname}
    #         data.append(pro)
    # for a in data:
    #     print(a['pro1'])
    # data = {
    #     '1': {'name': 'dayang', 'age': 18},
    #     '2': {'name': 'dayang1', 'age': 28},
    # }
    data = {'name': 'dayang', 'age': 18}
    print(data['name'])
    # data = [
    #     {'name': 'dayang', 'age': 18},
    #     {'name': 'dayang1', 'age': 28},
    # ]
    # for r in data:
    #     print(r['name'])
    # addrow = {'name': 'xiaoyangyang', 'age': 100}
    # data.append(addrow)
    return render_template('test.html', data=data)
